package cn.mesmile.activity.config;

import cn.hutool.core.util.StrUtil;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityManager;

import java.util.List;

/**
 * @author zb
 * @date 2022/1/5 15:05
 * @Description 创建任务之前的拦截
 */
public class WfUserTaskActivityBehavior extends UserTaskActivityBehavior {

    public WfUserTaskActivityBehavior(UserTask userTask) {
        super(userTask);
    }

    @Override
    protected  void handleAssignments(TaskEntityManager taskEntityManager, String assignee, String owner, List<String> candidateUsers,
                      List<String> candidateGroups, TaskEntity task, ExpressionManager expressionManager, DelegateExecution execution){

        System.out.println("------------------分配任务处理人拦截器--------------------");
        System.out.println("------------------分配任务处理人拦截器--------------------");
        System.out.println("------------------分配任务处理人拦截器--------------------");

        // 兼容之前的配置
        if (StrUtil.isNotBlank(assignee) || candidateUsers.size() > 0 || candidateGroups.size() > 0) {
            super.handleAssignments(taskEntityManager, assignee, owner, candidateUsers, candidateGroups, task, expressionManager, execution);
            return;
        }
        // 获取用户，设置审批人
//        IWfProcessService wfProcessService = SpringUtil.getBean(IWfProcessService.class);
//        WfTaskUser taskUser = wfProcessService.getTaskUser(task.getProcessDefinitionId(), task.getProcessInstanceId(), task.getTaskDefinitionKey());
//
//        List<User> userList = taskUser.getUserList();
//        candidateUsers = taskUser.getCandidateUserIds();
//        candidateGroups = taskUser.getCandidateGroupIds();
//        assignee = taskUser.getAssignee();
//
//        if (ObjectUtil.isNotEmpty(userList)) {
//            if (userList.size() == 1) { // 唯一审核人
//                assignee = userList.get(0).getId() + "";
//            }
//        }
        // 设置任务审批人
        // 任务节点的id
//        String taskDefinitionKey = task.getTaskDefinitionKey();
//        String name = task.getName();
        // 【这里的判断条件只是临时的】
//        if (taskDefinitionKey.equals("") || name.equals("任务节点的名称")){
            // 设置审批人

            // 审批人，只有一个人的时候【单节点】
//            assignee = "zhangsan";

            // 设置多实例审批人,这里可以从数据库里面查询 自己配置的多个人
//            candidateUsers = Arrays.asList("zhangsan","lisi","wangwu");
//        }

        super.handleAssignments(taskEntityManager, assignee, owner, candidateUsers, candidateGroups, task, expressionManager, execution);
    }
}
